package com.party.web.controller.system;

import java.util.HashMap;
import java.util.List;
import java.util.Set;

import com.party.common.annotation.Anonymous;
import com.party.common.core.domain.entity.SysRole;
import com.party.common.core.domain.model.LoginXcxBody;
import com.party.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import com.party.common.constant.Constants;
import com.party.common.core.domain.AjaxResult;
import com.party.common.core.domain.entity.SysMenu;
import com.party.common.core.domain.entity.SysUser;
import com.party.common.core.domain.model.LoginBody;
import com.party.common.utils.SecurityUtils;
import com.party.framework.web.service.SysLoginService;
import com.party.framework.web.service.SysPermissionService;
import com.party.system.service.ISysMenuService;

import javax.annotation.security.PermitAll;

/**
 * 登录验证
 */
@RestController
public class SysLoginController {
    @Autowired
    private SysLoginService loginService;

    @Autowired
    private ISysMenuService menuService;

    @Autowired
    private SysPermissionService permissionService;

    @Anonymous
    @GetMapping("testHello")
    public String testHello() {
        return "hello world";
    }

    @Anonymous
    @GetMapping("getCheckInfo")
    public String getCheckInfo() {
        return "rnfQrxQOLxJORwu2";
    }
    /**
     * 根据账号获取用户角色信息
     */
    @Anonymous
    @GetMapping("/selectUserRoles")
    public AjaxResult selectUserRoles(String userName) {
        List<SysRole> sysRoles = loginService.selectUserRoles(userName);
        return AjaxResult.success(sysRoles);
    }

    /**
     * 根据账号获取用户角色信息
     */
    @GetMapping("/findLoginRoleId")
    public AjaxResult findLoginRoleId(String userId) {
        return AjaxResult.success(loginService.findLoginRoleId(userId));
    }
    /**
     * 登录方法
     */
    @PostMapping("/login")
    public AjaxResult login(@RequestBody LoginBody loginBody) {
        AjaxResult ajax = AjaxResult.success();
        // 生成令牌
        String token = loginService.login(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(),
                loginBody.getUuid(),loginBody.getRoleId());
        ajax.put(Constants.TOKEN, token);
        return ajax;
    }

    /**
     * 登录--------方法
     */
    @PostMapping("/loginXcx")
    public AjaxResult loginXcx(@RequestBody LoginXcxBody loginBody) throws Exception {
        // 生成令牌
        HashMap<String, String> hashMap = loginService.loginXcx(loginBody);
        return AjaxResult.success(hashMap);
    }


    /**
     * 获取用户信息
     *
     * @return 用户信息
     */
    @GetMapping("getInfo")
    public AjaxResult getInfo() {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        // 角色集合
        Set<String> roles = permissionService.getRolePermission(user);
        // 权限集合
        Set<String> permissions = permissionService.getMenuPermission(user);
        AjaxResult ajax = AjaxResult.success();
        ajax.put("user", user);
        ajax.put("roles", roles);
        ajax.put("permissions", permissions);
        return ajax;
    }

    /**
     * 获取路由信息
     *
     * @return 路由信息
     */
    @GetMapping("getRouters")
    public AjaxResult getRouters() {
        Long userId = SecurityUtils.getUserId();
        List<SysMenu> menus = menuService.selectMenuTreeByUserId(userId);
        return AjaxResult.success(menuService.buildMenus(menus));
    }

    /**
     * 获取路由信息
     *
     * @return 路由信息
     */
    @GetMapping("getRoutersByRole")
    public AjaxResult getRoutersByRole() {
        Long userId = SecurityUtils.getUserId();
        String roleId = loginService.findLoginRoleId(userId.toString());
        long rId = 0;
        if (StringUtils.isNotEmpty(roleId)){
            rId = Long.parseLong(roleId);
        }
        List<SysMenu> menus = menuService.selectMenuTreeByUserIdAndRole(userId,rId);
        return AjaxResult.success(menuService.buildMenus(menus));
    }
}
